package com.ohos.apollo_hos.slice;

import com.apollographql.apollo.rxhos.simple.base.BaseModel;
import com.apollographql.apollo.rxhos.simple.base.IModelLisenner;
import com.apollographql.apollo.rxhos.simple.bean.BaseBean;
import com.apollographql.apollo.rxhos.simple.bean.TodoAny;
import com.ohos.apollo_hos.ResourceTable;
import com.ohos.apollo_hos.adapter.NewsListAdapter;
import com.ohos.apollo_hos.bean.MainBean;
import com.ohos.apollo_hos.loading.LoadingUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.miscservices.timeutility.Time;

/**
 * 详情页面
 */
public class DetailAbilitySlice extends AbilitySlice {
    private Button btnBookNow;
    private MainBean mainBean;
    private Image imgIcon;
    private Text txTitle, txName, txContent;
    private boolean isHasEmail = false;
    private boolean boolBookNow = false;
    private BaseModel baseModel;
    private int first = 0;
    Image imgLoading;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        baseModel = new BaseModel();
        baseModel.init();
        mainBean = intent.getSerializableParam("item");
//        LoadingUtil.showDoadling(DetailAbilitySlice.this);
        waiting();
    }

    private void initLayout() {
        LoadingUtil.hideDiading(DetailAbilitySlice.this, ResourceTable.Layout_detail_main);
        btnBookNow = (Button) findComponentById(ResourceTable.Id_btn_book_now);
        imgIcon = (Image) findComponentById(ResourceTable.Id_img_icon);
        txTitle = (Text) findComponentById(ResourceTable.Id_tx_title);
        txName = (Text) findComponentById(ResourceTable.Id_tx_name);
        txContent = (Text) findComponentById(ResourceTable.Id_tx_content);
        imgLoading = (Image) findComponentById(ResourceTable.Id_img_loading);
        imgIcon.setPixelMap(NewsListAdapter.imgs[mainBean.getImg()]);
        txTitle.setText(mainBean.getTitle());
        txContent.setText(mainBean.getContent());
        txName.setText("\uD83D\uDE80" + NewsListAdapter.imgs[mainBean.getImg()] + "");
        loadData();
    }

    @Override
    public void onActive() {
        super.onActive();
        if (first > 0) {
            loadData();
        }
        first++;
    }

    private void loadData() {
        DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); // context入参类型为ohos.app.Context。
        Preferences preferences = databaseHelper.getPreferences("apollo");
        isHasEmail = preferences.getString("email", null) != null;
        if (preferences.getBoolean("email" + mainBean.getImg(), false)) {
            btnBookNow.setText("CANCEL");

        } else {
            btnBookNow.setText("BOOK NOW");

        }
        btnBookNow.setClickedListener(component -> {
            if (isHasEmail) {
                btnBookNow.setVisibility(Component.HIDE);
                todoBookNow();
            } else {
                Intent intent1 = new Intent();
                present(new LoginAbilitySlice(), intent1);
            }
        });
    }

    private void waiting() {
//        if (first > 0) {
        LoadingUtil.showDoadling(DetailAbilitySlice.this);
//        }

        baseModel.todo(new TodoAny(0, 0, () -> {
            Time.sleep(LoadingUtil.WAITING_TIME);
            return new BaseBean();
        }));
        baseModel.setiModelLisenner(new IModelLisenner() {
            @Override
            public void onHosComplete(int flag) {

            }

            @Override
            public void onHosNext(BaseBean bean) {
                initLayout();
            }

            @Override
            public void onHosError(int flag, Throwable e) {

            }
        });
    }

    private void todoBookNow() {
        LoadingUtil.showImageDoadling(imgLoading);
        baseModel.todo(new TodoAny(0, 0, () -> {
            Time.sleep(LoadingUtil.WAITING_TIME);
            return new BaseBean();
        }));
        baseModel.setiModelLisenner(new IModelLisenner() {
            @Override
            public void onHosComplete(int flag) {

            }

            @Override
            public void onHosNext(BaseBean bean) {
                LoadingUtil.hideImageDiading(imgLoading);
                btnBookNow.setVisibility(Component.VISIBLE);
                if (boolBookNow) {
                    btnBookNow.setText("BOOK NOW");
                    LoadingUtil.showToast(DetailAbilitySlice.this, "A trip has been cancelled");
                    boolBookNow = false;
                } else {
                    btnBookNow.setText("CANCEL");
                    LoadingUtil.showToast(DetailAbilitySlice.this, "1 trip(s)`has(have) been booked");
                    boolBookNow = true;
                }

                DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); // context入参类型为ohos.app.Context。
                Preferences preferences = databaseHelper.getPreferences("apollo");
                preferences.putBoolean("email" + mainBean.getImg(), boolBookNow);
                preferences.flushSync();
            }

            @Override
            public void onHosError(int flag, Throwable e) {

            }
        });
    }
}
